home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectPlay / Maze / MazeCommon / MazeClient.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  6.9 KB  |  201 lines

  1. //----------------------------------------------------------------------------
  2. // File: mazeclient.h
  3. //
  4. // Desc: see main.cpp
  5. //
  6. // Copyright (c) 1999-2001 Microsoft Corp. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #ifndef _MAZE_CLIENT_H
  9. #define _MAZE_CLIENT_H
  10.  
  11.  
  12.  
  13.  
  14. //-----------------------------------------------------------------------------
  15. // Name: 
  16. // Desc: 
  17. //-----------------------------------------------------------------------------
  18. #include "DXUtil.h"
  19. #include "Maze.h"
  20. #include "NetAbstract.h"
  21. #include "Packets.h"
  22. #include "Trig.h"
  23. #include "SimpleStack.h"
  24. #include "Packets.h"
  25. #include "MazeServer.h"
  26. #include "IMazeGraphics.h"
  27.  
  28. class CMazeApp;
  29.  
  30. // The MAZE_CLIENT_VERSION should be rev'ed whenever the client exposes 
  31. // new functionality that the server expects.  This number is sent to
  32. // the server so the server can accept or reject the client based on its version.
  33. #define MAZE_CLIENT_VERSION         107
  34.  
  35.  
  36.  
  37. //-----------------------------------------------------------------------------
  38. // Name: 
  39. // Desc: 
  40. //-----------------------------------------------------------------------------
  41. struct PlayerObject
  42. {
  43.     DWORD           dwID;
  44.     D3DXVECTOR3     vPos;
  45.     ANGLE           aCameraYaw;
  46.     WORD            wCellX;
  47.     WORD            wCellY;
  48.     FLOAT           fLastValidTime;
  49.     PlayerObject*  pNext;
  50. };
  51.  
  52.  
  53.  
  54.  
  55. //-----------------------------------------------------------------------------
  56. // Name: 
  57. // Desc: 
  58. //-----------------------------------------------------------------------------
  59. #define MAZE_WIDTH  128
  60. #define MAZE_HEIGHT 128
  61. #define MAZE_SIZE   (MAZE_WIDTH*MAZE_HEIGHT)
  62.  
  63.  
  64.  
  65.  
  66. //-----------------------------------------------------------------------------
  67. // Name: 
  68. // Desc: 
  69. //-----------------------------------------------------------------------------
  70. class CMazeClient : public INetClient
  71. {
  72. public:
  73.     CMazeClient();
  74.     ~CMazeClient();
  75.  
  76.     void SetApp( CMazeApp* pMazeApp ) { m_pMazeApp = pMazeApp; };
  77.  
  78.     // INetClient
  79.     virtual HRESULT OnPacket( DWORD from , void* data , DWORD size );
  80.     virtual void    OnSessionLost( DWORD dwReason );
  81.  
  82.     // Connect an outbound network provider
  83.     void            SetOutboundClient( IOutboundClient* poutnet ) { m_pNet = poutnet; };
  84.  
  85.     HRESULT         Init( CMazeApp* pMazeApp, IMazeGraphics* pMazeGraphics );
  86.     HRESULT         Reset();
  87.     void            Shutdown();
  88.     void            Update( FLOAT elapsed );
  89.  
  90.     // Lock and unlock the world database
  91.     void            LockWorld() { m_WorldLock.Enter(); };
  92.     void            UnlockWorld() { m_WorldLock.Leave(); };
  93.  
  94.     // Lock and unlock the world database
  95.     void            SetMazeReady( BOOL bReady ) { if( bReady ) SetEvent( m_hReady ); else ResetEvent( m_hReady ); };
  96.     BOOL            IsMazeReady() { if( WaitForSingleObject( m_hReady, 0 ) == WAIT_OBJECT_0 ) return TRUE; else return FALSE; };
  97.  
  98.     // Check to see if we have received first Connect Config Packet.
  99.     void            SetFirstConfig( BOOL bReady ) { if( bReady ) SetEvent( m_hGotFirstConfig ); else ResetEvent( m_hGotFirstConfig ); };
  100.     BOOL            GotFirstConfig() { if( WaitForSingleObject( m_hGotFirstConfig, 0 ) == WAIT_OBJECT_0 ) return TRUE; else return FALSE; };
  101.  
  102.     // Get data useful for the engine (current position, etc. etc.)
  103.     D3DXVECTOR3     GetCameraPos() const { return m_vCameraPos; };
  104.     ANGLE           GetCameraYaw() const { return m_aCameraYaw; };
  105.     DWORD           GetNumPlayerObjects() const { return m_dwNumPlayerObjects; };
  106.  
  107.     // Get first engine PlayerObject is a cell
  108.     // NOTE: The engine must lock the world DB before traversing the cells
  109.     PlayerObject*   GetFirstPlayerObjectInCell( DWORD x, DWORD z );
  110.  
  111.     // The layout of the maze (engine needs this to draw)
  112.     CMaze           m_Maze;
  113.  
  114.     // Get network stats
  115.     DWORD           GetThroughputBPS();
  116.     DWORD           GetRoundTripLatencyMS();
  117.     void            GetPlayerStats( DWORD* pdwNumPlayers, DWORD* pdwNumNearbyPlayers ) { m_StatLock.Enter(); *pdwNumPlayers = m_dwNumPlayers; *pdwNumNearbyPlayers = m_dwNumNearbyPlayers; m_StatLock.Leave(); };
  118.     void            SetPlayerStats( DWORD dwNumPlayers, DWORD dwNumNearbyPlayers )     { m_StatLock.Enter(); m_dwNumPlayers = dwNumPlayers;   m_dwNumNearbyPlayers = dwNumNearbyPlayers;   m_StatLock.Leave(); };
  119.     DWORD           GetLocalClientID() const { return m_dwLocalClientID; };
  120.  
  121.     // Autopilot
  122.     void    EngageAutopilot( BOOL engage );
  123.     void    SetAutopilot(BOOL engage) { m_bEngageAutopilot = engage; };
  124.     BOOL    IsAutopilot() const { return m_bAutopilot; };
  125.  
  126.     // Set whether or not we have input focus
  127.     void    SetInputFocus( BOOL havefocus ) { m_bHaveInputFocus = havefocus; };
  128.  
  129. protected:
  130.     CMazeApp*       m_pMazeApp;
  131.     IMazeGraphics*  m_pMazeGraphics;
  132.  
  133.     D3DXVECTOR3     m_vCameraPos;
  134.     ANGLE           m_aCameraYaw;
  135.  
  136.     DWORD           m_dwNumPlayerObjects;
  137.  
  138.     CRandom         m_Rand;
  139.  
  140.     IOutboundClient* m_pNet;
  141.  
  142.     FLOAT           m_fLastOutboundTime;
  143.  
  144.     DWORD           m_dwNumPlayers;
  145.     DWORD           m_dwNumNearbyPlayers;
  146.     DWORD           m_dwLocalClientID;
  147.  
  148.     BOOL            m_bHaveInputFocus;
  149.  
  150.     void            SendPacket( ClientPacket* packet , DWORD size , BOOL guaranteed, DWORD dwTimeout );
  151.  
  152.     BOOL            IsValidPackSize( DWORD dwSize );
  153.  
  154.     // Arrays of cells and PlayerObjects (this consitutes the world DB), with associated lock
  155.     PlayerObject*       m_pctCells[SERVER_MAX_HEIGHT][SERVER_MAX_WIDTH];
  156.     PlayerObject        m_PlayerObjects[MAX_PLAYER_OBJECTS];
  157.     CCriticalSection    m_WorldLock;
  158.     CCriticalSection    m_StatLock;
  159.     HANDLE              m_hReady;
  160.     
  161.  
  162.     // Autopilot stuff
  163.     struct  AutopilotCell
  164.     {
  165.         AutopilotCell() {};
  166.         AutopilotCell( BYTE X , BYTE Y ) : x(X),y(Y) {};
  167.         BYTE   x,y;
  168.     };
  169.     SimpleStack<AutopilotCell,MAZE_SIZE>    m_AutopilotStack;
  170.     BYTE                                   m_AutopilotVisited[MAZE_HEIGHT][MAZE_WIDTH];
  171.     BOOL            m_bAutopilot;
  172.     BOOL            m_bEngageAutopilot;
  173.     D3DXVECTOR3     m_vAutopilotTarget;
  174.     ANGLE           m_aAutopilotTargetAngle;
  175.  
  176.     void            DoAutopilot( FLOAT elapsed );
  177.     void            DoManualPilot( FLOAT elapsed ); 
  178.     void            PickAutopilotTarget();
  179.  
  180.     void    HandlePlayerObjectsInAckPacket( ServerAckPacket* ppacket );
  181.  
  182.     void    AddPlayerObjectToCells( PlayerObject* pPlayerObject );
  183.     void    RemovePlayerObjectFromCells( PlayerObject* pPlayerObject );
  184.     BOOL    IsPlayerObjectInCell( DWORD wCellX, DWORD wCellY, PlayerObject* pPlayerObject );
  185.  
  186.     HANDLE              m_hGotFirstConfig;
  187.  
  188.     ClientNetConfig     m_NetConfig;
  189.     CRandom             m_NetRandom;
  190.     BOOL                m_bDoneInit;
  191.  
  192. private:
  193.     CMazeClient( const CMazeClient& );
  194.     void operator=( const CMazeClient& );
  195. };
  196.  
  197.  
  198.  
  199.  
  200. #endif
  201.